package logic

import (
	"context"
	"errors"
	"novel-mgr1/model"
	"novel-mgr1/userrpc/internal/svc"
	"novel-mgr1/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelAddLogic {
	return &NovelAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 发布小说
func (l *NovelAddLogic) NovelAdd(in *userrpc.NovelAddRequest) (*userrpc.NovelAddResponse, error) {
	// todo: add your logic here and delete this line
	var findUser model.User
	err := findUser.FindUserById(model.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("请登录")
	}
	novel := model.Novel{
		NovelTitle: in.NovelTitle,
		NovelClass: in.NovelClass,
		NovelInfo:  in.NovelInfo,
	}
	if in.NovelTitle == "" && in.NovelClass == "" && in.NovelInfo == "" {
		return nil, errors.New("不能为空")
	}
	if err := novel.NovelAdd(model.DB); err != nil {
		return nil, errors.New("发布失败")
	}
	return &userrpc.NovelAddResponse{
		NovelId: int64(novel.ID),
	}, nil
}
